#include <iostream>
#include <vector>
#include <memory>
#include <string>

// 抽象基类，代表图形元素
class Graphic {
public:
    virtual void draw() const = 0; 			// 绘制图形的接口
    virtual void move(int x, int y) = 0; 		// 移动图形元素到新的位置
    virtual ~Graphic() {}
};

// 单一的图形元素：圆形
class Circle : public Graphic {
private:
    int x, y, radius;
public:
    Circle(int x, int y, int radius) : x(x), y(y), radius(radius) {}
    void draw() const override {
        std::cout << "Draw Circle at (" << x << ", " << y << ") with radius " << radius << "\n";
    }
    void move(int newX, int newY) override {
        x = newX;
        y = newY;
    }
};

// 单一的图形元素：矩形
class Rectangle : public Graphic {
private:
    int x, y, width, height;
public:
    Rectangle(int x, int y, int width, int height) : x(x), y(y), width(width), height(height) {}
    void draw() const override {
        std::cout << "Draw Rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << "\n";
    }
    void move(int newX, int newY) override {
        x = newX;
        y = newY;
    }
};

// 组合类：可以包含多个图形元素，包括其他组合
class CompositeGraphic : public Graphic {
private:
    std::vector<std::shared_ptr<Graphic>> m_children;
public:
    // 添加图形到组合中
    void add(const std::shared_ptr<Graphic>& graphic) {
        m_children.push_back(graphic);
    }
    // 绘制组合图形，即绘制其包含的所有图形元素
    void draw() const override {
        std::cout << "Draw CompositeGraphic with " << m_children.size() << " children:\n";
        for (const auto& child : m_children) {
            child->draw();						// 调用每个元素的绘制方法
        }
    }
    // 移动组合图形的位置，即移动其包含的所有图形元素
    void move(int newX, int newY) override {
        for (const auto& child : m_children) {
            child->move(newX, newY); 			// 移动每个元素到新位置
        }
    }
};
// 装饰器基类
class Decorator : public Graphic {
protected:
    std::shared_ptr<Graphic> m_component;
public:
    Decorator(const std::shared_ptr<Graphic>& component) : m_component(component) {}
    void draw() const override {
        m_component->draw(); 					// 调用被装饰对象的绘制方法，由子类实现装饰
    }
    void move(int x, int y) override {
        m_component->move(x, y); 				// 调用被装饰对象的移动方法，由子类实现装饰
    }
};
// 具体的装饰器：边框装饰器
class BorderDecorator : public Decorator {
private:
    void addBorder() const {
        std::cout << " with Border" << std::endl;
    }
public:
    BorderDecorator(const std::shared_ptr<Graphic>& component) : Decorator(component) {}
    void draw() const override {
        Decorator::draw(); 					// 调用被装饰对象的绘制方法
        addBorder(); 							// 添加边框
    }
};
// 具体的装饰器：颜色滤镜装饰器
class ColorDecorator : public Decorator {
private:
    std::string m_color;
    void addColor() const {
        std::cout << " with Color " << m_color << std::endl;
    }
public:
    ColorDecorator(const std::shared_ptr<Graphic>& component, const std::string& color) : Decorator(component), m_color(color) {}
    void draw() const override {
        Decorator::draw(); 					// 调用被装饰对象的绘制方法
        addColor(); 							// 添加颜色滤镜
    }
};
int main() {
    // 创建图形
    auto circle = std::make_shared<Circle>(10, 10, 5);  
    auto rectangle = std::make_shared<Rectangle>(10, 20, 5, 6);  
    auto compositeGraphic = std::make_shared<CompositeGraphic>();  

    // 添加简单图形到组合中
    compositeGraphic->add(circle);
    compositeGraphic->add(rectangle);

    // 绘制原始组合图形
    std::cout << "Original positions:\n";
    compositeGraphic->draw();

    // 移动组合图形中的所有元素
    compositeGraphic->move(50, 50);
    std::cout << "\nAfter moving the composite graphic:\n";
    compositeGraphic->draw();

    // 使用装饰器增强图形
    auto borderedCircle = std::make_shared<BorderDecorator>(circle);
    auto colorFilteredCircle = std::make_shared<ColorDecorator>(circle, "red");

    // 绘制装饰后的图形
    std::cout << "\nDecorated circles:\n";
    borderedCircle->draw();
    colorFilteredCircle->draw();

    // 移动装饰后的图形
    colorFilteredCircle->move(70, 70);

    borderedCircle->draw();

    return 0;
}